import numpy as np


def generate_random_data():
    """生成随机数据并计算结果"""
    # 设置随机种子以确保结果可重现
    np.random.seed()

    # 生成步幅d (正态分布，均值20，范围10-30)
    while True:
        d = np.random.normal(20, 5)
        if 10 <= d <= 30:
            break

    # 生成步高h (均匀分布，范围5-15)
    h = np.random.uniform(5, 15)

    # 生成时间间隔t (均匀分布，范围0.02-0.5)
    t = np.random.uniform(0.02, 0.05)

    # 计算抛物线参数 a = -h/d²
    a = -h / (d ** 2)

    # 定义导数函数 f'(x) = 2*a*x
    def df(x):
        return 2 * a * x

    # 定义弧长积分函数的被积函数 √(1 + [f'(x)]²)
    def integrand(x):
        return np.sqrt(1 + df(x) ** 2)

    # 使用梯形法则进行数值积分计算弧长，积分区间为 [-d, d]
    x = np.linspace(-d, d, 1000)
    y = integrand(x)
    s = np.trapz(y, x)
    s = round(s, 6)

    # 计算点数n (二次曲线)
    n = int((s / 5) // 1) * 2

    # 计算T和N
    T = n * t
    N = int(np.ceil(4000 / d))

    # 计算总时间
    time = N * T

    # 确定结果
    result = "成功" if time <= 150 else "失败"

    return {
        'd': round(d, 2),
        'h': round(h, 2),
        't': round(t, 2),
        'n': n,
        'result': result
    }


# 示例调用
if __name__ == "__main__":
    data = generate_random_data()
    print("步幅d:", data['d'])
    print("步高h:", data['h'])
    print("时间间隔t:", data['t'])
    print("点数n:", data['n'])
    print("结果result:", data['result'])